// 帮助工具对象

import { Toast } from 'vant';
import Storage from "@/utils/storage";

const Tool = {
    toast: function(msg, duration){ //轻提示
        Toast({
            message: msg,
            duration: duration || 1500,
            forbidClick: true
        })
    },
    validator: { //校验器
        isEmpty(val) { //空值
            return val ? true : false;
        },
        isMobilePhone(val) { //手机号
            let regexPhone = /^1[3456789]\d{9}$/;
            return regexPhone.test(val) ? true : false;
        },
        isMsgCode(val) { //验证码
            let regexCode = /\d{4}/;
            return regexCode.test(val) ? true : false;
        },
        isPassword(val) { //密码
            let regexPassword = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/;
            return regexPassword.test(val) ? true : false;
        },
        isTradePassword(val) { //交易密码
            let regexPassword = /\d{6}/;
            return regexPassword.test(val) ? true : false;
        },
        isIdentityCard(val) { //身份证
            let regexIdentityCard = /(^\d{15}$)|(^\d{17}([0-9]|X|x)$)/;
            return regexIdentityCard.test(val) ? true : false;
        },
        isChinese(val) { //姓名
            let regexChinese = /^[\u4e00-\u9fa5]{2,5}$/;
            return regexChinese.test(val) ? true : false;
        },
        isSpecialText(val) { //特殊字符
            let regexSpecialText = /^[A-z0-9\u4e00-\u9fa5]*$/;
            return regexSpecialText.test(val) ? true : false;
        },
        isMoney(val) { //金额保留2位小数
            let regexMoney = /^\d+(\.\d{0,2})?$/;
            return regexMoney.test(val) ? true : false;
        }
    },
    addEventListenerInputFocusout: function(){ //解决IOS中input失焦后，页面上移问题
        let flag;
        let scorllFn;
        let isIOS = /(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent);
        if (isIOS) {
            document.body.addEventListener('focusin', () => {  //软键盘弹起事件
                flag = true;
                clearTimeout(scorllFn);
            })
            document.body.addEventListener('focusout', () => { //软键盘关闭事件
                flag = false;
                if (!flag) {
                    scorllFn = setTimeout(function () {
                        window.scrollTo({ 
                            top: 0, 
                            left: 0, 
                            behavior: "smooth" //平滑滚动
                        })
                    }, 200);
                } else {
                    return
                }
            })
        } else {
            return
        }
    },
    userLoginFlag: function(){ //用户登录状态
        return Storage.get('token') ? true : false;
    },
    dateFormat: function(date, type){ //日期转换 date格式：2019-09-23T11:59:54
        let dateBase = '';
        if(date.indexOf('T') != -1) {
            dateBase = date.replace('T', ' ');
        }else {
            dateBase = date
        }
        
        switch(type){
            case 0: //年月日
                return dateBase.replace(' ', '日 ').split(' ')[0].replace('-', '年').replace('-', '月');
            case 1: //年月日 时分秒
                return dateBase.replace(' ', '日 ').split(' ')[0].replace('-', '年').replace('-', '月') + ' ' + dateBase.replace(' ', '日 ').split(' ')[1];
            case 2: //年月日 上/下午 时分
                let judgeTime = dateBase.split(' ')[1].split(':');
                let timeText = Number(judgeTime[0]) > 11 ? '下午' : '上午';
                return dateBase.replace(' ', '日 ').split(' ')[0].replace('-', '年').replace('-', '月') + ' ' + timeText + judgeTime[0] + ':' + judgeTime[1];
            case 3: //年月日 时分
                let judgeTimeOne = dateBase.split(' ')[1].split(':');
                return dateBase.replace(' ', '日 ').split(' ')[0].replace('-', '年').replace('-', '月') + ' ' + judgeTimeOne[0] + ':' + judgeTimeOne[1];
        }
    },
    textFormat: function(number){ //数字转繁写
        let newNumer = number.toString()
        let newNumerLength = newNumer.length
        let returnNum = '';
        switch(newNumerLength){
            case 1:
                returnNum = Tool.transfromNum(number)
                break;
            case 2:
                let oneNum2 = Tool.transfromNum(newNumer.split('')[0])
                let twoNum2 = Tool.transfromNum(newNumer.split('')[1])
                if(twoNum2 == '零'){ //90
                    returnNum = oneNum2 + '拾'
                }else{ //99
                    returnNum = oneNum2 + '拾' + twoNum2
                }
                break;
            case 3:
                let oneNum3 = Tool.transfromNum(newNumer.split('')[0])
                let twoNum3 = Tool.transfromNum(newNumer.split('')[1])
                let threeNum3 = Tool.transfromNum(newNumer.split('')[2])
                if(twoNum3 == '零'){
                    if(threeNum3 == '零'){ //900
                        returnNum = oneNum3 + '佰'
                    }else{ //909
                        returnNum = oneNum3 + '佰零' + threeNum3
                    }
                }else{
                    if(threeNum3 == '零'){ //990
                        returnNum = oneNum3 + '佰' + twoNum3 + '拾'
                    }else{ //999
                        returnNum = oneNum3 + '佰' + twoNum3 + '拾' + threeNum3
                    }
                }
                break;
            case 4:
                let oneNum4 = Tool.transfromNum(newNumer.split('')[0])
                let twoNum4 = Tool.transfromNum(newNumer.split('')[1])
                let threeNum4 = Tool.transfromNum(newNumer.split('')[2])
                let fourNum4 = Tool.transfromNum(newNumer.split('')[3])
                if(twoNum4 == '零'){
                    if(threeNum4 == '零'){
                        if(fourNum4 == '零'){ //9000
                            returnNum = oneNum4 + '仟'
                        }else{ //9009
                            returnNum = oneNum4 + '仟零' + fourNum4
                        }
                    }else{
                        if(fourNum4 == '零'){ //9090
                            returnNum = oneNum4 + '仟零' + threeNum4 + '拾'
                        }else{ //9099
                            returnNum = oneNum4 + '仟零' + threeNum4 + '拾' + fourNum4
                        }
                    }
                }else{
                    if(threeNum4 == '零'){
                        if(fourNum4 == '零'){ //9900
                            returnNum = oneNum4 + '仟' + twoNum4 + '佰'
                        }else{ //9909
                            returnNum = oneNum4 + '仟' + twoNum4 + '佰零' + fourNum4
                        }
                    }else{
                        if(fourNum4 == '零'){ //9990
                            returnNum = oneNum4 + '仟' + twoNum4 + '佰' + threeNum4 + '拾'
                        }else{ //9999
                            returnNum = oneNum4 + '仟' + twoNum4 + '佰' + threeNum4 + '拾' + fourNum4
                        }
                    }
                }
                break;
            case 5:
                let oneNum5 = Tool.transfromNum(newNumer.split('')[0])
                let twoNum5 = Tool.transfromNum(newNumer.split('')[1])
                let threeNum5 = Tool.transfromNum(newNumer.split('')[2])
                let fourNum5 = Tool.transfromNum(newNumer.split('')[3])
                let fiveNum5 = Tool.transfromNum(newNumer.split('')[4])
                if(twoNum5 == '零') {
                    if(threeNum5 == '零') {
                        if(fourNum5 == '零') {
                            if(fiveNum5 == '零') {
                                returnNum = oneNum5 + '萬'
                            }else{
                                returnNum = oneNum5 + '萬零' + fiveNum5
                            }
                        }else{
                            if(fiveNum5 == '零') {
                                returnNum = oneNum5 + '萬零' + fourNum5 + '拾'
                            }else{
                                returnNum = oneNum5 + '萬零' + fourNum5 + '拾' + fiveNum5
                            }
                        }
                    }else{
                        if(fourNum5 == '零') {
                            if(fiveNum5 == '零') {
                                returnNum = oneNum5 + '萬零' + threeNum5 + '佰'
                            }else{
                                returnNum = oneNum5 + '萬零' + threeNum5 + '佰零' + fiveNum5
                            }
                        }else{
                            if(fiveNum5 == '零') {
                                returnNum = oneNum5 + '萬零' + threeNum5 + '佰' + fourNum5 + '拾'
                            }else{
                                returnNum = oneNum5 + '萬零' + threeNum5 + '佰' + fourNum5 + '拾' + fiveNum5
                            }
                        }
                    }
                }else{
                    if(threeNum5 == '零') {
                        if(fourNum5 == '零') {
                            if(fiveNum5 == '零') {
                                returnNum = oneNum5 + '萬' + twoNum5 + '仟'
                            }else{
                                returnNum = oneNum5 + '萬' + twoNum5 + '仟零' + fiveNum5
                            }
                        }else{
                            if(fiveNum5 == '零') {
                                returnNum = oneNum5 + '萬' + twoNum5 + '仟零' + fourNum5 + '拾'
                            }else{
                                returnNum = oneNum5 + '萬' + twoNum5 + '仟零' + fourNum5 + '拾' + fiveNum5
                            }
                        }
                    }else{
                        if(fourNum5 == '零') {
                            if(fiveNum5 == '零') {
                                returnNum = oneNum5 + '萬' + twoNum5 + '仟' + threeNum5 + '佰'
                            }else{
                                returnNum = oneNum5 + '萬' + twoNum5 + '仟' + threeNum5 + '佰零' + fiveNum5
                            }
                        }else{
                            if(fiveNum5 == '零') {
                                returnNum = oneNum5 + '萬' + twoNum5 + '仟' + threeNum5 + '佰' + fourNum5 + '拾'
                            }else{
                                returnNum = oneNum5 + '萬' + twoNum5 + '仟' + threeNum5 + '佰' + fourNum5 + '拾' + fiveNum5
                            }
                        }
                    }
                }
                break;
            case 6:
                returnNum = '拾萬'
                break;
        }
        return returnNum;
    },
    transfromNum(number) { //数字转换
        let num = Number(number)
        let backNum = '';
        switch(num) {
            case 0:
                backNum = '零';
                break;
            case 1:
                backNum = '壹';
                break;
            case 2:
                backNum = '贰';
                break;
            case 3:
                backNum = '叁';
                break;
            case 4:
                backNum = '肆';
                break;
            case 5:
                backNum = '伍';
                break;
            case 6:
                backNum = '陆';
                break;
            case 7:
                backNum = '柒';
                break;
            case 8:
                backNum = '捌';
                break;
            case 9:
                backNum = '玖';
                break;
        }
        return backNum;
    }
}

export default Tool;